<template>
  <di style="padding: 20px;width: 80%">
    <h2 style="margin-left: 20px;margin-top: 0px">编辑图书</h2>
    <!--  数据表单  -->
    <el-form :rules="rules" ref="ruleForm" :inline="true" :model="form" style="width: 100%" label-width="100px">
      <el-form-item label="图书标准码" prop="bookNo">
        <el-select v-model="form.bookNo" clearable filterable placeholder="请选择" @change="selBook">
          <el-option
              v-for="item in books"
              :key="item.id"
              :label="item.bookNo"
              :value="item.bookNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="图书名称" prop="bookName" >
        <el-input v-model="form.bookName" placeholder="请输入标名称" disabled></el-input>
      </el-form-item>
      <el-form-item label="所需积分"  prop="score" >
        <el-input  v-model="form.score" disabled></el-input>
      </el-form-item>

      <br/>
      <el-form-item label="会员码" prop="userNo">
        <el-select v-model="form.userNo" clearable filterable placeholder="请选择" @change="selUser">
          <el-option
              v-for="item in users"
              :key="item.id"
              :label="item.username"
              :value="item.username">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input disabled v-model="form.userName" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="用户联系方式" prop="userPhone">
        <el-input disabled v-model="form.userPhone" placeholder="请输入联系方式"></el-input>
      </el-form-item>
      <el-form-item label="用户账户积分" prop="account">
        <el-input disabled v-model="form.account" ></el-input>
      </el-form-item>
    </el-form>

    <!-- 按键 -->
    <div style="text-align: center; margin-top: 30px">
      <el-button type="primary" @click="save" size="mediu">提交</el-button>
      <!--      <el-button type="danger">取消</el-button>-->
    </div>
  </di>
</template>

<script>
import request from "@/utils/request";
export default {
  name: "EditBook.vue",
  data() {
    return {
      form: {},
      books: [],
      users: [],
      rules: {
        bookNo: [
          {required : true, message: '请输入图书标准码', trigger: 'blur'},
        ],
        userNo: [
          {required : true, message: '请输入会员码', trigger: 'blur'},
        ],
      }
    }
  },
  created() {
    request.get('/book/list').then(res => {
      this.books = res.data
    })

    request.get('/user/list').then(res => {
      this.users = res.data.filter( v => v.status )
    })

    const id = this.$route.query.id
    request.get("/borrow/" + id).then(res => {
      this.form = res.data
      if (this.form.category) {
        this.form.categories = this.form.category.split(' > ')
      }
    })
  },
  methods: {
    save() {
      request.put('/borrow/update',this.form).then(res => {
        if (res.code === '200') {
          this.$notify.success('更新成功')
          this.$router.push("/borrowList")
        }else {
          this.$notify.error(res.msg)
        }
      })
    },
    selBook() {
      const  book = this.books.find(v => v.bookNo === this.form.bookNo)
      request.get('/book/' + book.id).then(res => {
        this.$set(this.form, 'bookName', res.data.name)
        this.form.score = res.data.score
        this.form.nums = res.data.nums
      })
    },
    selUser() {
      const  user = this.users.find(v => v.username === this.form.userNo)
      request.get('/user/' + user.id).then(res => {
        this.$set(this.form, 'userName', res.data.name)
        this.form.userPhone = res.data.phone
        this.form.account = res.data.account
      })
    }
  }
}
</script>
